package com.wanli.agent.homepage;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.IncomeListBean;
import com.wanli.agent.bean.ReturncashListBean;
import com.wanli.agent.event.IncomeDataEvent;
import com.wanli.agent.homepage.adapter.IncomeShareListAdapter;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.utils.NumberFormatUtils;
import com.wanli.agent.widget.AppTitleBar;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IncomeDetailsActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_order_money)
    TextView tvOrderMoney;
    @BindView(R.id.tv_pay_type)
    TextView tvPayType;
    @BindView(R.id.tv_income_money)
    TextView tvIncomeMoney;
    @BindView(R.id.ll_share)
    LinearLayout llShare;
    @BindView(R.id.tv_jihuo_money)
    TextView tvJihuoMoney;
    @BindView(R.id.ll_return)
    LinearLayout llReturn;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_relation_type)
    TextView tvRelationType;
    @BindView(R.id.ll_share_new)
    LinearLayout llShareNew;
    private String type;
    private IncomeListBean.DataBean.ItemListBean dataBean;
    private List<ReturncashListBean.DataBean.ShareMoneyDetailBean> beanList;
    private IncomeShareListAdapter listAdapter;
    private IncomeDataEvent dataEvent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_income_details);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        type = getIntent().getStringExtra("type");
        dataEvent = EventBus.getDefault().getStickyEvent(IncomeDataEvent.class);
        if (dataEvent != null) {
            EventBus.getDefault().removeStickyEvent(dataEvent);
            dataBean = dataEvent.getDataBean();
        }
        if (dataBean != null) {
            tvName.setText(dataBean.getStore_name());
            if ("new_share".equals(type)) {
                llShareNew.setVisibility(View.VISIBLE);
                llShare.setVisibility(View.VISIBLE);
                llReturn.setVisibility(View.GONE);
                String order_fee = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getOrder_fee()) / 100);
                tvOrderMoney.setText(order_fee + "元");
                if (dataBean.getPay_category_id() == 0) {
                    tvPayType.setText("现金支付");
                } else if (dataBean.getPay_category_id() == 1) {
                    tvPayType.setText("微信");
                } else if (dataBean.getPay_category_id() == 2) {
                    tvPayType.setText("支付宝");
                } else if (dataBean.getPay_category_id() == 3) {
                    tvPayType.setText("云闪付");
                } else if (dataBean.getPay_category_id() == 5) {
                    tvPayType.setText("NFC");
                } else if (dataBean.getPay_category_id() == 6) {
                    tvPayType.setText("刷卡");
                }
                String shareMoney = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getMoney()) / 100);
                tvIncomeMoney.setText(shareMoney + "元");

                if (dataBean.getRelation_type() == 1) {
                    tvRelationType.setText("交易分润");
                } else if (dataBean.getRelation_type() == 2) {
                    tvRelationType.setText("提现奖励");
                } else if (dataBean.getRelation_type() == 3) {
                    tvRelationType.setText("激活返现");
                } else if (dataBean.getRelation_type() == 4) {
                    tvRelationType.setText("挂靠奖励");
                } else if (dataBean.getRelation_type() == 9) {
                    tvRelationType.setText("交易分润");
                } else {
                    tvRelationType.setText("");
                }
            } else if ("share".equals(type)) {
                llShare.setVisibility(View.VISIBLE);
                llReturn.setVisibility(View.GONE);
                String order_fee = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getOrder_fee()) / 100);
                tvOrderMoney.setText(order_fee + "元");
                if (dataBean.getPay_category_id() == 0) {
                    tvPayType.setText("现金支付");
                } else if (dataBean.getPay_category_id() == 1) {
                    tvPayType.setText("微信");
                } else if (dataBean.getPay_category_id() == 2) {
                    tvPayType.setText("支付宝");
                } else if (dataBean.getPay_category_id() == 3) {
                    tvPayType.setText("云闪付");
                } else if (dataBean.getPay_category_id() == 5) {
                    tvPayType.setText("NFC");
                } else if (dataBean.getPay_category_id() == 6) {
                    tvPayType.setText("刷卡");
                }
                String shareMoney = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getShare_money()) / 100000000);
                tvIncomeMoney.setText(shareMoney + "元");

            } else {
                llShare.setVisibility(View.GONE);
                llReturn.setVisibility(View.VISIBLE);
                String order_fee = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getOrder_fee()) / 100);
                tvJihuoMoney.setText(order_fee + "元");

                beanList = new ArrayList<>();
                listAdapter = new IncomeShareListAdapter(beanList);
                recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
                recyclerView.setAdapter(listAdapter);
                new HomePageModelImpl().requestReturncashOrder(dataBean.getOrder_no(), new DataCallBack<ReturncashListBean>() {
                    @Override
                    public void onSuccessful(ReturncashListBean bean) {
                        beanList.addAll(bean.getData().getShare_money_detail());
                        listAdapter.notifyDataSetChanged();
                        tvName.setText(bean.getData().getMerchant_name());
                    }

                    @Override
                    public void onFailed(String msg, String code) {

                    }
                });
            }
        }
    }
}
